<?php

namespace App\Admin\Controllers\Datashow;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Models\ReportForms\ReportForms;

class ReportFormsController extends Controller
{
    public function getlistqoq(Request $request)
    {
        $type = (int)($request->type??0); //1业务员2钢厂3客户名称4材质5规格
        $cpid = $request->cpid ?? 0; //默认是0 查询全部
        $data_type = (int)($request->data_type ?? 1);   //1出库量2入库量3销售业绩4销售毛利

        // 新增验证逻辑
        if ( $type < 1 || $type > 5) {
            return response()->json(['code' => '400', 'msg' => 'type范围错误（1-5）']);
        }

        // 新增验证逻辑
        if (empty($data_type) || $data_type < 1 || $data_type > 5) {
            return response()->json(['code' => '400', 'msg' => 'data_type为空或者（1-5）']);
        }
        // 3. 联动规则验证
        if (in_array($type, [2, 4, 5]) && $data_type === 4) {
            return response()->json([
                'code' => '400',
                'msg' => 'type='.$type.'时data_type不能为4'
            ]);
        }


        $list = ReportForms::getListYear($request,$cpid);

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function getlistchain(Request $request)
    {
        $type = (int)($request->type??0);
        $cpid = $request->cpid ?? 0; //默认是0 查询全部

        $data_type = (int)($request->data_type ?? 1);

        // 新增验证逻辑
        if (empty($type) || $type < 1 || $type > 5) {
            return response()->json(['code' => '400', 'msg' => 'type为空或者范围错误（1-5）']);
        }

        // 新增验证逻辑
        if (empty($data_type) || $data_type < 1 || $data_type > 5) {
            return response()->json(['code' => '400', 'msg' => 'data_type为空或者（1-5）']);
        }
        // 3. 联动规则验证
        if (in_array($type, [2, 4, 5]) && $data_type === 4) {
            return response()->json([
                'code' => '400',
                'msg' => 'type='.$type.'时data_type不能为4'
            ]);
        }

        $list = ReportForms::getListMonth($request,$cpid);

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function getPriceTrend(Request $request)
    {
        $cpid = $request->cpid ?? 0; //默认是0 查询全部

        $data_type = $request->data_type ?? 0;
        $type = $request->type ?? 0;

        // 新增验证逻辑
        if ($data_type < 0 || $data_type > 5) {
            return response()->json(['code' => '400', 'msg' => 'data_type（1-5）']);
        }
        if($type == 0)
        {
            $list = ReportForms::getPriceTrend($request,$cpid);
        }else{
            $list = ReportForms::getPriceTrend2($request,$cpid);
        }

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function getPurchaseOverview(Request $request)
    {
        $cpid = $request->cpid ?? 0; //默认是0 查询全部

        $list = ReportForms::getPurchaseOverview($request,$cpid);

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }

    public function getSaleOverview(Request $request)
    {
        $cpid = $request->cpid ?? 0; //默认是0 查询全部

        $list = ReportForms::getSaleOverview($request,$cpid);

        return response()->json(['code' => '000', 'msg' => '操作成功', 'data' => $list]);
    }
}
